package io.wongxd.solution.compose.custom.views.other

import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import io.wongxd.solution.compose.custom.views.util.rememberMutableStateOf

/**
 * 测量compose帧数
 * 建议仅测试时使用
 * 参考: https://juejin.cn/post/7090554700344328229
 */
@Composable
//@Deprecated("建议仅测试时使用")
fun FpsText(modifier: Modifier = Modifier) {
    var fpsCount by rememberMutableStateOf(0)
    var fps by rememberMutableStateOf(0)
    var lastUpdate by rememberMutableStateOf(0L)
    Text(
        text = "Fps: $fps",
        modifier = modifier,
        color = Color.Red,
        style = MaterialTheme.typography.body1
    )

    LaunchedEffect(Unit) {
        while (true) {
            withFrameMillis { ms ->
                fpsCount++
                if (fpsCount == 5) {
                    fps = (5000 / (ms - lastUpdate)).toInt()
                    lastUpdate = ms
                    fpsCount = 0
                }
            }
        }
    }
}